package com.roger.tool.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

public class FileUtil {

    
    public static void writeFileData(Context m_context, String fileName, String message)
	{ 
    	if (m_context == null) {
    		Log.d("FileUtil", "m_context is null, error");
    		return;
    	}
    	File f = new File(m_context.getFilesDir() + File.separator + fileName);
    	if (f.exists()) {// 判断是否存在这个文件
    		f.delete();// 如果存在就删掉
    	}
    	try{
	        FileOutputStream fout = m_context.openFileOutput(fileName, Activity.MODE_PRIVATE);
	        byte [] bytes = message.getBytes(); 
	        fout.write(bytes); 
	        fout.close(); 
	    } 
    	catch(Exception e){ 
	        e.printStackTrace(); 
    	} 
	}
    
    public static String readFileData(Context m_context, String fileName)
    { 
        String res=""; 
      
        File f = new File(m_context.getFilesDir() + File.separator + fileName);
        if(!f.exists()){// 判断是否存在这个文件
        	return res;
        }
        try { 
        	FileInputStream fin = m_context.openFileInput(fileName); 
            int length = fin.available(); 
            byte [] buffer = new byte[length]; 
            fin.read(buffer);     
            res = EncodingUtils.getString(buffer, "UTF-8"); 
            fin.close(); 
        } 
        catch(Exception e){ 
        	e.printStackTrace(); 
        } 

        return res; 
    }
	
	
}
